{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python3710jvsc74a57bd053c72d0039ada9c1d34dd1733f66130047a118ae04fa085bf08fa89049564cc7",
   "display_name": "Python 3.7.10 64-bit ('cv_env': conda)"
  },
  "metadata": {
   "interpreter": {
    "hash": "53c72d0039ada9c1d34dd1733f66130047a118ae04fa085bf08fa89049564cc7"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "source": [
    "Create an image using OpenCV"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [],
   "source": [
    "# white background \n",
    "# np.unint8 gives unsigned integer from 0 to 255\n",
    "image = np.ones((480,640,3),np.uint8)*255"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [],
   "source": [
    "cv2.imshow('image',image)\n",
    "cv2.waitKey()\n",
    "cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [],
   "source": [
    "triangle_pnts = np.array([(319,50),(165,160),(476,160)])\n",
    "image = cv2.drawContours(image, [triangle_pnts],0,(0,0,255),-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [],
   "source": [
    "cv2.imshow('image',image)\n",
    "cv2.waitKey()\n",
    "cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [],
   "source": [
    "bgr_rectangle = (0,255,0)\n",
    "start_point = (213,160)\n",
    "end_point = (426,360)\n",
    "image = cv2.rectangle(image,start_point,end_point,bgr_rectangle,-1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [],
   "source": [
    "cv2.imshow('image',image)\n",
    "cv2.waitKey()\n",
    "cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [],
   "source": [
    "bgr_door = (255,0,0)\n",
    "start_point = (284,240)\n",
    "end_point = (355,360)\n",
    "image = cv2.rectangle(image,start_point,end_point,bgr_door,-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [],
   "source": [
    "cv2.imshow('image',image)\n",
    "cv2.waitKey()\n",
    "cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [],
   "source": [
    "bgr_window = (255,126,0)\n",
    "start_point = (255,180)\n",
    "end_point = (305,210)\n",
    "image = cv2.rectangle(image,start_point,end_point,bgr_window,-1)\n",
    "\n",
    "bgr_window = (255,126,0)\n",
    "start_point = (339,180)\n",
    "end_point = (389,210)\n",
    "image = cv2.rectangle(image,start_point,end_point,bgr_window,-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {},
   "outputs": [],
   "source": [
    "cv2.imshow('image',image)\n",
    "cv2.waitKey()\n",
    "cv2.destroyAllWindows()"
   ]
  },
  {
   "source": [
    "Crop images and swap the cropped part in two images."
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "image_one = cv2.imread('image_one.jpg')\n",
    "image_two = cv2.imread('image_two.jpg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "#[rows, cols]\n",
    "image_one_crop = image_one[256:512,240:720].copy()\n",
    "image_two_crop = image_two[256:512,240:720].copy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "image_one_crop, image_two_crop = image_two_crop, image_one_crop"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "cv2.imshow('image_1_crop',image_one_crop)\n",
    "cv2.waitKey()\n",
    "cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "cv2.imshow('image_2_crop',image_two_crop)\n",
    "cv2.waitKey()\n",
    "cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "image_one[256:512,240:720] = image_one_crop\n",
    "image_two[256:512,240:720] = image_two_crop"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "cv2.imshow('image',image_one)\n",
    "cv2.waitKey()\n",
    "cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "cv2.imshow('image',image_two)\n",
    "cv2.waitKey()\n",
    "cv2.destroyAllWindows()"
   ]
  },
  {
   "source": [
    "Combine images."
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Joining images\n",
    "image_1 = cv2.imread('image_one.jpg')\n",
    "image_2 = cv2.imread('image_two.jpg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [],
   "source": [
    "image_join_1 = np.hstack([image_1,image_2])\n",
    "image_join_2 = np.hstack([image_2,image_1])\n",
    "image_join = np.vstack([image_join_1,image_join_2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "(2160, 3840, 3)"
      ]
     },
     "metadata": {},
     "execution_count": 40
    }
   ],
   "source": [
    "image_join.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [],
   "source": [
    "dims = (image_1.shape[1],image_1.shape[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [],
   "source": [
    "# resampling the pixels \n",
    "resized = cv2.resize(image_join,dims,interpolation=cv2.INTER_AREA)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "(1920, 1080)"
      ]
     },
     "metadata": {},
     "execution_count": 48
    }
   ],
   "source": [
    "dims"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [],
   "source": [
    "cv2.imshow('image',resized)\n",
    "cv2.waitKey()\n",
    "cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ]
}